local diversionSkill = fk.CreateSkill{
  name = "iberia__diversion_skill",
}

local I = require "packages/iberia/utility"

diversionSkill:addEffect("cardskill", {
  prompt = "#iberia__diversion_skill",
  can_use = function (self, player, card)
    return not player:prohibitUse(card)
  end,
  distance_limit = 1,
  mod_target_filter = function(self, player, to_select, selected, card, extra_data)
    if to_select ~= player then
      if extra_data and extra_data.bypass_distances then
        return true
      else
        return self:withinDistanceLimit(player, false, card, to_select)
      end
    end
    return false
  end,
  target_filter = Util.CardTargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    if effect.to.dead or effect.from:isKongcheng() then return end
    local others = room:getOtherPlayers(effect.to, false)
    if #others == 0 then return end

    local plist, cid = room:askToChooseCardsAndPlayers(effect.from, {
      targets = others,
      max_num = 1,
      min_num = 1,
      max_card_num = 1,
      min_card_num = 1,
      pattern = ".|.|.|hand",
      prompt = "#iberia__diversion-give::" .. effect.to.id,
      skill_name = "iberia__diversion",
      cancelable = false
    })

    room:moveCardTo(cid, Player.Hand, effect.to, fk.ReasonGive, "iberia__diversion", nil, false, effect.from)
    local target = plist[1]
    local card, reason = nil, fk.ReasonGive
    if #effect.to:getCardIds{Player.Hand, Player.Equip} <= 2 then
      card = effect.to:getCardIds{Player.Hand, Player.Equip}
    elseif I.isCounsellor(effect.from) and not I.isCounsellor(effect.to) then
      card = room:askToChooseCards(target, {
        min = 2,
        max = 2,
        flag = "he",
        skill_name = "iberia__diversion",
        target = effect.to
      })
      reason = fk.ReasonPrey
    else
      card = room:askToCards(effect.to, {
        max_num = 2,
        min_num = 2,
        include_equip = true,
        skill_name = "iberia__diversion",
        cancelable = false,
        prompt = "#iberia__diversion-give2::" .. target.id
      })
    end

    room:moveCardTo(card, Player.Hand, target, reason, "iberia__diversion", nil, false, effect.from)
  end
})

return diversionSkill
